﻿Imports System.Data.SqlClient
Public Class Category
    Dim cmd As New SqlCommand
    Public Sub refreshdata()
        Try
            grdCategory.Rows.Clear()
            cmd.Connection = cn
            cmd.CommandText = "select * from tblCategory where CategoryName like N'%" & txtsearch.Text & "%' OR CategoryID like N'%" & txtsearch.Text & "%'"
            Dim rd As SqlDataReader = cmd.ExecuteReader
            While rd.Read = True
                Me.grdCategory.Rows.Add(rd(0), rd(1), rd(2))
            End While
            rd.Close()
        Catch ex As Exception
        End Try
    End Sub

    Private Sub Category_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        condb()
        refreshdata()
        Main.lblheader.Text = "ប្រភេទមុខម្ហូប​ និងផលិតផលផ្សេងៗ"
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Try
            cmd = New SqlCommand
            cmd.Connection = cn
            cmd.CommandText = "update tblCategory set CategoryName =N'" & txtcategoryname.Text & "',Description =N'" & txtdescription.Text & "' where CategoryID ='" & txtid.Text & "'"
            cmd.ExecuteNonQuery()
            MessageBox.Show("Update Success")
            refreshdata()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Try
            If grdCategory.SelectedRows.Count = 0 Then
                Exit Sub
            End If
            If MessageBox.Show("Are you want to delete", "Delete Confirme", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.No Then
                Exit Sub
            End If
            For Each row In grdCategory.SelectedRows
                Dim id = row.Cells("categoryid").Value
                cmd.Connection = cn
                cmd.CommandText = "delete tblCategory where CategoryID=" & id
                cmd.ExecuteNonQuery()
            Next
            refreshdata()
        Catch ex As Exception
            MessageBox.Show(ex.Message)

        End Try
    End Sub

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        txtdescription.Clear()
        txtid.Clear()
        txtcategoryname.Clear()
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        Try
            cmd = New SqlCommand
            cmd.Connection = cn
            cmd.CommandText = "INSERT INTO tblCategory VALUES (N'" & txtcategoryname.Text & "',N'" & txtdescription.Text & "')"
            cmd.ExecuteNonQuery()
            refreshdata()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub grdtable_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles grdCategory.SelectionChanged
        Try
            If grdCategory.SelectedRows.Count = 0 Then
                Exit Sub
            End If
            Me.txtid.Text = grdCategory.CurrentRow.Cells("Categoryid").Value
            Me.txtcategoryname.Text = grdCategory.CurrentRow.Cells("Categoryname").Value
            Me.txtdescription.Text = grdCategory.CurrentRow.Cells("discription").Value

        Catch ex As Exception
        End Try
    End Sub

    Private Sub txtsearch_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtsearch.TextChanged
        refreshdata()
    End Sub

    Private Sub grdCategory_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles grdCategory.CellContentClick

    End Sub
End Class